package com.collect.senegal.zhiScene.zhiDs.zhiRoot;

import android.annotation.SuppressLint;
import android.util.Log;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.senegal.R;
import com.collect.senegal.zhiBase.BaseActivity;
import com.collect.senegal.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.senegal.zhiHelper.ZHIPhoneCallHelper;
import com.collect.senegal.zhiScene.zhiDs.zhiAccount.ZHIDsAccountFragment;
import com.collect.senegal.zhiScene.zhiDs.zhiFollow.ZHIDsFollowFragment;
import com.collect.senegal.zhiScene.zhiWidget.zhiNavigationBar.ZHIBottomTabBarItem;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_ROOT_DS)
public class ZHIRootDsActivity extends BaseActivity implements ZHIRootDsFragmentInterface {

    @BindView(R.id.bar_loan)
    ZHIBottomTabBarItem bar_loan;

    @BindView(R.id.bar_account)
    ZHIBottomTabBarItem bar_account;

    private FragmentManager mFragmentManager;
    private ZHIDsFollowFragment mZHIDsFollowFragment;
    private ZHIDsAccountFragment mZHIDsAccountFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_root_ds;
    }

    @Override
    protected void initInjector() {
//        mActivityComponent.inject(this);
    }

    @Override
    protected void initView() {
        mFragmentManager = getSupportFragmentManager();

        initEvent();
        selecFragment(0);

        //初始化电话模块
        ZHIPhoneCallHelper.getInstance().initPhoneCall(getActivity(), ZHIPhoneCallHelper.ZHIPhoneCallType.KFD0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ============");
    }

    @Override
    protected void onDestroy() {
        try {
            //移除电话模块
            ZHIPhoneCallHelper.getInstance().stopPhoneCall(getActivity());
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

    @Override
    public void onShowFilterFollow() {

    }

    public void initEvent() {
        bar_loan.setCFOnMenuItemDidChangedListener(item -> {
            bar_account.setChecked(false);
            selecFragment(0);
        });
        bar_account.setCFOnMenuItemDidChangedListener(item -> {
            bar_loan.setChecked(false);
            selecFragment(1);
        });
    }

    private void selecFragment(int indext) {
        FragmentTransaction fragemntTransaction = mFragmentManager.beginTransaction();
        hideFragment(fragemntTransaction);
        switch (indext) {
            case 0: {
                bar_loan.setChecked(true);
                bar_account.setChecked(false);
                if (null == mZHIDsFollowFragment) {
                    mZHIDsFollowFragment = new ZHIDsFollowFragment();
                    fragemntTransaction.add(R.id.content, mZHIDsFollowFragment);
                } else {
                    fragemntTransaction.show(mZHIDsFollowFragment);
                }
                break;
            }
            case 1: {
                bar_loan.setChecked(false);
                bar_account.setChecked(true);
                if (null == mZHIDsAccountFragment) {
                    mZHIDsAccountFragment = new ZHIDsAccountFragment();
                    fragemntTransaction.add(R.id.content, mZHIDsAccountFragment);
                } else {
                    fragemntTransaction.show(mZHIDsAccountFragment);
                }
                break;
            }
            default:
                break;
        }
        fragemntTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragemntTransaction) {
        if (fragemntTransaction == null) return;
        if (null != mZHIDsFollowFragment) {
            fragemntTransaction.hide(mZHIDsFollowFragment);
        }
        if (null != mZHIDsAccountFragment) {
            fragemntTransaction.hide(mZHIDsAccountFragment);
        }
    }
}